<?php
$this->breadcrumbs=array(
	'Inventory Items'=>array('inventory/items'),
	"{$model->code} - {$model->short_description}" => array("item/view/id/{$model->id}"),
	'Bill Of Materials'
);
?>
<script type="text/javascript">

window.dhx_globalImgPath="<?php echo $this->createUrl("/images"); ?>/";


var itemCodeData = <?php echo $item_codes; ?>;
var itemNameData = <?php echo $item_names; ?>;

$(document).ready(function() {

	var container = $('#add-component-container');
	var dec_places = 4;
	
	// New Componenet Form
	$('a.butPrimary:contains("Add Component")').click(function() {
		showAddComponentForm();
		return false;
	});

	// Cancel Componenet Form
	$('a.butMedium:contains("Cancel")').click(function() {
		hideAddComponentForm();
		return false;
	});

	function showAddComponentForm() {
		$('#add-component-container').show();
		$('a.butPrimary:contains("Add Component")').hide();
		return false;
	}

	function hideAddComponentForm() {
		codeCombobox.closeAll();
		codeCombobox.selectOption(0);
		
		nameCombobox.closeAll();
		nameCombobox.selectOption(0);

		$("input[name=component_quantity]").val('');
		
		$('#add-component-container').hide();
		$('a.butPrimary:contains("Add Component")').show();
		return false;
	}

	// Save form functions
	$('a.butMedium:contains("Add")').click(function() {

		var itemId = $("input[name=item_id]").val();
		var componentId = $("input[name=component_id]").val();
		var componentQuantity = $("input[name=component_quantity]").val();

		var dataString = 'Component[item_id]='+itemId+'&Component[component_id]='+componentId+'&Component[quantity]='+componentQuantity;

		hideAddComponentForm();

		$.ajax({  
			type: "POST",  
			url: '<?php echo $this->createUrl("ajax/addcomponent"); ?>',  
			data: dataString, 
			dataType: 'json', 
			success: function(response) {  
				
			
				var row = compenentRowContent(response);

				// TODO: would like to make this fade in at some point.
				$('table.index tbody').prepend(row);

				var new_amount = Number($('#total-component-amount').html()) + Number(response.total);
				
				$('#total-component-amount').html(new_amount.toFixed(dec_places));

				
				
				return false;
			},
			error: function(response) {
				alert('error'+response.responseText);
			}  

		});  

		return false;
	});

	compenentRowContent = function(data) {
		
		var tpl = $('#component-table-row').html();
		var tpl1 = '';


		tpl = tpl.replace(/{id}/g, data.id);
		tpl = tpl.replace('{code}', data.code);
		tpl = tpl.replace('{name}', data.name);
		tpl = tpl.replace('{category}', data.category);
		tpl = tpl.replace('{type}', data.type);
		tpl = tpl.replace('{quantity}', Number(data.quantity).toFixed(2));
		tpl = tpl.replace('{cost}', Number(data.cost).toFixed(dec_places));
		tpl = tpl.replace('{total}', Number(data.total).toFixed(dec_places));

		return tpl;
	}

	

	// Item Code combobox
	var itemCodeList = [];
	
	$.each(itemCodeData, function(id, code) {
		itemCodeList.push({text: code.htmlentities(), value: id});
	});
	
	var comboContainer = container.find('div.item_code-container')[0];
	
	codeCombobox = new dhtmlXCombo(comboContainer, 'component_code', '133px');
	codeCombobox.enableFilteringMode(true);
	codeCombobox.addOption(itemCodeList);
	
	$(codeCombobox.DOMelem_input).attr('maxlength', 25).addClass('expense-vendor');

	codeCombobox.attachEvent('onSelectionChange', function() {
		selectText = itemNameData[codeCombobox.getSelectedValue()];
		nameCombobox.setComboText(selectText);
		$("input[name=component_id]").val(codeCombobox.getSelectedValue());
	});

	// Item Name combobox
	var itemNameList = [];
	
	$.each(itemNameData, function(id, name) {
		itemNameList.push({text: name.htmlentities(), value: id});
	});
	
	var comboContainer = container.find('div.item_name-container')[0];
	
	nameCombobox = new dhtmlXCombo(comboContainer, 'component_name', '133px');
	nameCombobox.enableFilteringMode(true);
	nameCombobox.addOption(itemNameList);

	$(nameCombobox.DOMelem_input).attr('maxlength', 25).addClass('expense-vendor');

	nameCombobox.attachEvent('onSelectionChange', function() {
		selectedText = itemCodeData[nameCombobox.getSelectedValue()];
		codeCombobox.setComboText(selectedText);
		$("input[name=component_id]").val(nameCombobox.getSelectedValue());
	});


});
	
</script>

<script type="text/html" id="component-table-row">
				<tr>
					<td class="col-overflow-left"></td>
					<td align='center'><input class="checkbox" type='checkbox' name='item[]' value='{id}'></td>
					<td><a href="<?php echo $this->createUrl("item/view/id/"); ?>/{id}" >{code}</a></td>
					<td>{name}</td>
					<td>{category}</td>
					<td>{type}</td>
					<td>{quantity}</td>
					<td align="right">${cost}</td>
					<td align="right">${total}</td>
					<td class="col-overflow-right"></td>
				</tr>
</script>

<h2 class="pt">Bill Of Materials: <?php echo $model->code; ?></h2><a href="#" class="butPrimary"><span>Add Component</span></a>

<div class="clearb"></div>

<div class="action-bar">
	<div id="add-component-container" style="display:none;">
	
		<div style="margin-left:40px;">
	
		<table cellspacing="0" cellpadding="0" width="100%" class="newTable pad">
		<tbody>
				<tr>
					<td>
					
					<div class="shelf">
						<!-- Item Code -->
						<div class="span-7 append-half">
							<label  class="formTitle no-pointer" for="{prefix}expense_vendor">Item Code</label>
			
							<div class="item_code-container"> 
			
							</div>
			
						</div>
						
						<!-- Item Name -->
						<div class="span-7 append-half">
							<label  class="formTitle no-pointer" for="{prefix}expense_vendor">Item Name</label>
			
							<div class="item_name-container"> 
			
							</div>
			
						</div>
						
						<!-- Quantity Needed -->
						<div class="span-7 append-half">
							<label for="component_quantity" class="formTitle no-pointer">Qnty Needed</label><br>
							<input type="text" class="expense-amount" value="" name="component_quantity" style="width: 64px;">
						</div>
					
						
					</div>
					
					</td>
				</tr>
			</tbody>
		</table>
		</div>
		<input type="hidden" name="item_id" value="<?php echo $model->id; ?>" />
		<input type="hidden" name="component_id" value="<?php echo $model->id; ?>" />
		<table align="center" style="margin-top: 10px; margin-bottom:5px;">
			<tbody>
				<tr>
					<td> 
					<a href="#" class="butMedium"><span>Add</span></a>
					<a href="#" class="butMedium"><span>Cancel</span></a>
					</td>
				</tr>
			</tbody>
		</table>
	</div>
	<div class="actions">

		<ul>
			<li><span><a href="#" onclick="return SetAction('delete')">Delete</a></span></li>
		</ul>
						
		<div class="clBoth"></div>
	</div>
</div> 
<form method="post" action="<?php echo $this->createUrl("BillOfMaterials/delete"); ?>">
<input type="hidden" name="form_action" value="" />
<input type="hidden" name="itemId" value="<?php echo $model->id; ?>" />
<div class="index-wrapper">
	<table class="index">	
		<thead>
		
			<tr>
				<td class="col-overflow-left">
					<img height="1" width="40" src="<?php Yii::app()->request->baseUrl; ?>/images/spacer.gif" alt="" />
				</td>
				<td class="col-checkbox">
					<input onClick="return Check_Uncheck(this, 'item[]')" class="checkbox"  type='checkbox' name='check'>
				</td>
				<td align="left" width='90'><a href="">Item Code </a></td>
				<td align="left" width='100'><a href="">Short Desc</a></td>
				<td align="left" width='150'><a href="">Category </a></td>
				<td align="left" width='100'><a href="">Type </a></td>
				<td align="left" width='100'><a href="">Qnty Needed</a></td>
				<td align="right" width='80'><a href="">Cost</a></td>
				<td align="right"><a href="">Total</a></td>
				<td class="col-overflow-right">
					<img height="1" width="40" src="<?php Yii::app()->request->baseUrl; ?>/images/spacer.gif" alt="" />
				</td>
			</tr>
		
		</thead>

		<tbody>
			<?php 
			$component_total = 0;
			foreach ($componentList as $bom) : 
				
				$component = $bom->component;

				$quantity = $bom->quantity;
				$cost = $component->getCost();
				$total = $cost * $quantity;
				$component_total += $total;
			?>
				<tr>
					<td class="col-overflow-left"></td>
					<td align='center'><input class="checkbox" type='checkbox' name='component[]' value='<?php echo $bom->reference_id; ?>'></td>
					<td><a href="<?php echo $this->createUrl("item/view/id/{$component->id}"); ?>" ><?php echo $component->code; ?></a></td>
					<td><?php echo $component->short_description; ?></td>
					<td><?php echo $component->itemCategory->category; ?></td>
					<td><?php echo $component->itemType->type; ?></td>
					<td><?php echo $quantity; ?></td>
					<td align="right">$<?php echo number_format($cost, 4); ?></td>
					<td align="right">$<?php echo number_format($total,4); ?></td>
					<td class="col-overflow-right"></td>
				</tr>
			<?php endforeach; ?>
		</tbody>
	</table>
	<div id="page" style=" margin: 10px 40px;">
		<div class="totals" style="text-align: right;">
			<strong>Component Total:</strong> <span id="total-component-amount"><?php echo number_format($component_total,4); ?></span>
			<span class="currency-code currency-code-block">&nbsp;USD</span>
		</div>
	</div>			
	</div>	
</div>
</form><!-- form -->